﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RM2012Server.Business;

namespace RM2012Server.Model
{
    public class AreaDAO
    {
        DataServices m_AreaDAO = new DataServices();

        public DataTable GetAll()
        {
            m_AreaDAO.Load("GetAllArea", null);
            return m_AreaDAO;
        }

        public DataTable GetById(String areaId)
        {
            SqlParameter param = new SqlParameter("@AreaId", SqlDbType.VarChar, 6);
            param.Value = areaId;

            m_AreaDAO.Load("GetAreaById", param);
            return m_AreaDAO;
        }

        public DataTable GetByLikeId(String areaId)
        {
            SqlParameter param = new SqlParameter("@AreaId", SqlDbType.VarChar, 6);
            param.Value = areaId;

            m_AreaDAO.Load("GetAreaByLikeId", param);
            return m_AreaDAO;
        }

        public DataTable GetByName(String areaName)
        {
            SqlParameter param = new SqlParameter("@AreaName", SqlDbType.NVarChar, 100);
            param.Value = areaName;

            m_AreaDAO.Load("GetAreaByName", param);
            return m_AreaDAO;
        }

        public String GetIdByName(String name)
        {
            SqlParameter param = new SqlParameter("@AreaName", SqlDbType.NVarChar, 100);
            param.Value = name;

            return m_AreaDAO.ExecuteScalar("GetAreaIdByName", param).ToString();
        }

        public int Add(AreaInfo info)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@AreaId", SqlDbType.VarChar, 6);
            param[0].Value = info.AreaId;

            param[1] = new SqlParameter("@AreaName", SqlDbType.NVarChar, 100);
            param[1].Value = info.AreaName;

            param[2] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[2].Value = info.Remark;

            return m_AreaDAO.ExecuteUpdate("AddArea", param);
        }

        public int Update(AreaInfo info)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@AreaId", SqlDbType.VarChar, 6);
            param[0].Value = info.AreaId;

            param[1] = new SqlParameter("@AreaName", SqlDbType.NVarChar, 100);
            param[1].Value = info.AreaName;

            param[2] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[2].Value = info.Remark;

            return m_AreaDAO.ExecuteUpdate("UpdateArea", param);
        }

        public int Delete(String areaId)
        {
            SqlParameter param = new SqlParameter("@AreaId", SqlDbType.VarChar, 6);
            param.Value = areaId;

            return m_AreaDAO.ExecuteUpdate("DeleteArea", param);
        }

        public String GetLast()
        {
            return m_AreaDAO.ExecuteScalar("GetLastAreaId", null).ToString();
        }

        public DataRow AddNewRow()
        {
            return m_AreaDAO.NewRow();
        }

        public void AddNewArea(DataRow m_Row)
        {
            m_AreaDAO.Rows.Add(m_Row);
        }

        public bool SaveData()
        {
            return m_AreaDAO.ExecuteNoneQuery() > 0;
        }
    }
}
